package jatosoft.accounting.webapp.managedbean;

import jatosoft.accounting.dao.model.BaseModel;
import jatosoft.accounting.dao.model.Voucher;
import jatosoft.accounting.dao.model.VoucherItem;
import jatosoft.accounting.service.VoucherService;
import jatosoft.accounting.webapp.UIModel;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;

public class AddVoucherBean extends BaseManagedBean implements Serializable {

	private static final long serialVersionUID = -7319322253545341300L;

	private VoucherService voucherService;
	private SelectHelper selectHelper;
	private Integer voucherId;
	private Voucher voucher;
	private Date qryFromDate;
	private Date qryToDate;

	private List accountSummaries;

	public void setSelectHelper(SelectHelper selectHelper) {
		this.selectHelper = selectHelper;
	}

	public void setAccountSummaries(List accountSummaries) {
		this.accountSummaries = accountSummaries;
	}

	public void setVoucherService(VoucherService voucherService) {
		this.voucherService = voucherService;
	}

	public Integer getVoucherId() {
		return voucherId;
	}

	public void setVoucherId(Integer voucherId) {
		this.voucherId = voucherId;
	}

	public List getAccountSummaries() {
		return accountSummaries;
	}

	public Voucher getVoucher() {
		return voucher;
	}

	public void setVoucher(Voucher voucher) {
		this.voucher = voucher;
	}

	public Date getQryFromDate() {
		return qryFromDate;
	}

	public void setQryFromDate(Date qryFromDate) {
		this.qryFromDate = qryFromDate;
	}

	public Date getQryToDate() {
		return qryToDate;
	}

	public void setQryToDate(Date qryToDate) {
		this.qryToDate = qryToDate;
	}

	@PostConstruct
	public void initPage() {
		// initVoucher();
		// init default query date
		Calendar fDate = Calendar.getInstance();
		fDate.set(Calendar.DAY_OF_MONTH-1, 1);
		qryFromDate = fDate.getTime();
		Calendar tDate = Calendar.getInstance();
		tDate.set(Calendar.DAY_OF_MONTH, tDate
				.getMaximum(Calendar.DAY_OF_MONTH));
		qryToDate = tDate.getTime();
	}

	public String saveVoucher() {
		voucher.cleanVoucherItemList();
		if (voucher.isEmpty()) {
			addMessage("Voucher Items is EMPTY. NO voucher added.");
		} else if(voucher.getVoucherDate()==null){
			addMessage("Please enter Voucher Date !");
		} else {
			Integer voucherId = voucherService.addVoucher(voucher);
			addMessage("Voucher added successfully! Voucher ID is " + voucherId
					+ ".");
			initVoucher();
		}
		return "success";
	}

	public String queryVoucher() {
		dumpModel2UIModel(voucherService.queryVoucherByDuration(qryFromDate,
				qryToDate));
		accountSummaries = voucherService.summarizeByAccount(qryFromDate,
				qryToDate);
		return "list";
	}

	public String apportionTax() {
		selectHelper.clearAccountNameList();
		voucher = voucherService
				.summarizeTaxByMonth(Calendar.getInstance().get(Calendar.YEAR),
						Calendar.getInstance().get(Calendar.MONTH));
		userId = getLoginUserId();
		voucher.setCreator(userId);
		voucher.setModifier(userId);
		voucher.setCreationDate(new Date());
		voucher.setModificationDate(new Date());
		for (int i = 0; i < voucher.size(); i++) {
			selectHelper.updateAccountName(voucher.getVoucherItem(i).getSeq(),
					voucher.getVoucherItem(i).getAccount());
		}
		return "add";

	}

	public String copyVoucher() {
		if (getSelectModels().isEmpty()) {
			addMessage("Please select one Voucher !");
			return "listVoucher";
		} else if (getSelectModels().size() > 1) {
			addMessage("You can only select one Voucher !");
			return "listVoucher";
		} else {
			voucher = (Voucher) getSelectModels().get(0); 
			voucher = voucherService.getVoucher(voucher.getId());
	
//			voucher.setVoucherDate(new Date());
			userId = getLoginUserId();
			voucher.setCreator(userId);
			voucher.setModifier(userId);
			voucher.setCreationDate(new Date());
			voucher.setModificationDate(new Date());
			for (VoucherItem vi : voucher.getVoucherItemList()) {
				vi.setId(null);
			}
			for (int i = 0; i < voucher.size(); i++) {
				selectHelper.updateAccountName(voucher.getVoucherItem(i)
						.getSeq(), voucher.getVoucherItem(i).getAccount());
			}
			return "add";
		}
	}

	public String addVoucher() {
		initVoucher();
		return "add";
	}

	public String removeVoucher() {
		if (!getSelectModels().isEmpty()) {
			for (BaseModel v : getSelectModels()) {
				voucherService.removeVoucher((Voucher) v);
				
			}
//			Voucher v = (Voucher) getSelectModel();
//			voucherService.removeVoucher(v);
			dumpModel2UIModel(voucherService.queryVoucherByDuration(
					qryFromDate, qryToDate));
		} else {
			super.addMessage("NO VOUCHER selected !");
		}
		return "listVoucher";
	}

	public String back2List() {

		return "listVoucher";
	}

	public String account() {
		return "account";
	}

	public String showVoucherDetail() {
		voucher = voucherService.getVoucher(getLinkModelId());
		return "view";
	}

	void initVoucher() {
		selectHelper.clearAccountNameList();
		userId = getLoginUserId();
		voucher = new Voucher(10);
//		voucher.setVoucherDate(new Date());
		voucher.setCreator(userId);
		voucher.setModifier(userId);
		voucher.setCreationDate(new Date());
		voucher.setModificationDate(new Date());
	}
}
